package cn.mooncode.basketballManage.controller;


import cn.mooncode.basketballManage.pojo.Match;
import cn.mooncode.basketballManage.pojo.MatchTeamSpace;
import cn.mooncode.basketballManage.pojo.RespBean;
import cn.mooncode.basketballManage.pojo.RespPageBean;
import cn.mooncode.basketballManage.service.ICoachActiveService;
import cn.mooncode.basketballManage.service.IMatchService;
import cn.mooncode.basketballManage.service.IMatchTeamSpaceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/match")
public class MatchController {

    @Autowired
    private IMatchService matchService;
    @Autowired
    private IMatchTeamSpaceService matchTeamSpaceService;
    @Autowired
    private ICoachActiveService coachActiveService;

    @ApiOperation(value = "获取所有比赛信息")
    @GetMapping("/")
    public RespPageBean getAllMatches(@RequestParam(defaultValue = "1") Integer currentPage,
                                      @RequestParam(defaultValue = "10") Integer size, Match match) {
        return matchService.getAllMatches(currentPage, size, match);
    }

    @ApiOperation(value = "获取当前用户所有比赛信息")
    @GetMapping("/currentMatch")
    public RespPageBean getCurrentMatches(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "10") Integer size, Match match, @RequestParam Integer tid) {
        return matchService.getCurrentMatches(currentPage, size, match, tid);
    }

    @ApiOperation(value = "添加比赛")
    @PostMapping("/")
    @Transactional
    public RespBean addMatch(@RequestBody Match match) {
        System.out.println(match);
        if (matchService.save(match)) {
            MatchTeamSpace matchTeamSpace = new MatchTeamSpace();
            matchTeamSpace.setMid(match.getId());
            matchTeamSpace.setSid(match.getSpace().getId());
            matchTeamSpace.setTid1(match.getTeam1().getId());
            matchTeamSpace.setTid2(match.getTeam2().getId());
            if (matchTeamSpaceService.save(matchTeamSpace)) {
//                添加当前教练选择场地的点击数
                if (coachActiveService.addHits(match.getCid(), match.getSpace().getId())) {
                    return RespBean.success("添加成功！");
                }
            }
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "修改比赛信息")
    @PutMapping("/")
    @Transactional
    public RespBean updateMatch(@RequestBody Match match) {
        Integer mid = match.getId();
        Integer tid1 = match.getTeam1().getId();
        Integer tid2 = match.getTeam2().getId();
        Integer sid = match.getSpace().getId();
        MatchTeamSpace matchTeamSpace = new MatchTeamSpace();
        matchTeamSpace.setMid(mid);
        matchTeamSpace.setSid(sid);
        matchTeamSpace.setTid1(tid1);
        matchTeamSpace.setTid2(tid2);
        if (matchService.updateById(match)) {
            if (matchTeamSpaceService.update(matchTeamSpace, new QueryWrapper<MatchTeamSpace>().eq("mid", mid))) {
                return RespBean.success("修改成功！");
            }
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "删除比赛信息")
    @DeleteMapping("/{id}")
    @Transactional
    public RespBean deleteMatch(@PathVariable Integer id) {
        if (matchService.removeById(id)) {
            if (matchTeamSpaceService.remove(new QueryWrapper<MatchTeamSpace>().eq("mid", id))) {
                return RespBean.success("删除成功！");
            }
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "添加比赛结果")
    @PostMapping("/addResult")
    public RespBean addResult(@RequestParam Integer id, @RequestParam String result) {
        return matchService.addResult(id, result);
    }

}
